package net.optix.ip.commands
{
	import com.adobe.cairngorm.business.Responder;
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import net.optix.ip.business.*;
	import net.optix.ip.events.GetInventionEvent;
	import net.optix.ip.events.SetMaxBudgetEvent;
	import net.optix.ip.model.ModelLocator;
	
	import views.ViewInstance;

	public class SetMaxBudgetCommand implements Command, Responder
	{
		// attributes ============================
		private var model : ModelLocator = ModelLocator.getInstance();

	
		// functions ============================
		public function execute( event:CairngormEvent ) : void
		{
			var myEvent:SetMaxBudgetEvent = SetMaxBudgetEvent( event );
			
			var delegate:InventionDelegate = new InventionDelegate(  );     
			delegate.setMaxBudget( this , myEvent.vo );
			

		}
		
		//----------------------------------------------------------------------------
		public function onResult( event : * = null ) : void
		{		
			var title:String	=	"Result of recorded maximum budget";
			var message:String;
			
			try
			{
				var invention:XML			=	event.result;
				//trace(invention);
				var	result:int				=	invention.result;
				switch(result)
				{
					case -2:
						message	=	"Access Denied !";
						break;
					case -1:
						message	=	"The invention can't be editing now , may it is already be published !";
						break;
					case 0:
						message	=	"Program error!";
						break;
					case 1:
						message	=	"Maximum budget recorded!";
						var getInvention:GetInventionEvent	=	new GetInventionEvent(model.currentInvention.inventionID);
						com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(getInvention);
						break;
				}
			}
			catch(error:Error)
			{
				message	=	"Program error";
			}
			
			model.bSubmit	=	true;
			
			
			ViewInstance.showMessage("Result of update maximum budget for invention",message);
			
		}
		
		//----------------------------------------------------------------------------
		public function onFault( event : * = null ) : void
		{
			
			model.result	=	"Program error";
			
		}

	}
}